﻿using Abp.Domain.Repositories;
using Abp.Domain.Services;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace shenghua.Order.Refunds
{
    public class RefundManager: DomainService
    {
        private readonly IRepository<Refund> _refundResitory;

        public RefundManager(
            IRepository<Refund> refundResitory
            )
        {
            _refundResitory = refundResitory;
        }

        public async Task<Refund> Get(int id)
        {
            return await _refundResitory.GetAsync(id);
        }

        public async Task<Refund> Add(Orders.Order order, Refund refund)
        {
            refund.OrderId = order.Id;
            refund.OrderNumber = order.OrderNumber;
            refund.MemberId = order.MemberInfo.Id;
            await _refundResitory.InsertAndGetIdAsync(refund);
            return refund;
        }

        public async Task<Refund> Update(Refund refund)
        {
            await _refundResitory.UpdateAsync(refund);
            return refund;
        }

        public async Task Delete(int id)
        {
            await _refundResitory.DeleteAsync(id);
        }

        public async Task<List<Refund>> GetAllOfOrder(long? memberId, string orderNumber)
        {
            if(memberId != null)
            {
                return await _refundResitory.GetAllListAsync(r => r.MemberId == memberId && r.OrderNumber == orderNumber);
            }
            else
            {
                return await _refundResitory.GetAllListAsync(r => r.OrderNumber == orderNumber);
            }
        }

        public async Task<List<Refund>> GetAll(long memberId)
        {
            return await _refundResitory.GetAllListAsync(r => r.MemberId == memberId);
        }
    }
}
